<?php

namespace app\base\controller\api;

use app\base\service\MemberService;
use app\base\service\PaymentService;
use think\admin\Controller;
use app\base\model\MemberPlans;
use app\base\controller\api\Auth;
use app\base\model\MemberBeanPackages;
use app\base\model\MemberOrder as MemberOrderMod;
use think\admin\extend\CodeExtend;


/**
 * 会员分销
 * Class MemberShip
 * @package app\base\api\controller
 */
class Promotion extends Auth
{
    public function promotionCount()
    {

        $ai_user = $this->app->db->name('ai_user')->where(['id'=>$this->user_id])->find();
        $fx_code = $ai_user['fx_code'];
        $user_ids = $this->app->db->name('ai_user')->where(['register_code'=>$fx_code])->column('id');
        $amount = MemberOrderMod::mQuery()->where(['type'=>1,'create_type'=>1,'status'=>2])->whereIn('user_id',$user_ids)->sum('amount');
        $num = MemberOrderMod::mQuery()->where(['type'=>1,'create_type'=>1,'status'=>2])->whereIn('user_id',$user_ids)->count();
        $user_count = count($user_ids);
        $this->success('获取成功', ['amount'=>$amount,'num'=>$num,'user_count'=>$user_count,'fx_code'=>$fx_code]);
    }

    //订单列表
    public function getPromotionList(){
        $page= input('page',1);
        $page_size = input('page_size',20);
        $ai_user = $this->app->db->name('ai_user')->where(['id'=>$this->user_id])->find();
        $fx_code = $ai_user['fx_code'];
        $user_ids = $this->app->db->name('ai_user')->where(['register_code'=>$fx_code])->column('id');
        $list = MemberOrderMod::mQuery()->where(['type'=>1,'create_type'=>1,'status'=>2])->whereIn('user_id',$user_ids)->order('id desc')
            ->page($page,false,false,$page_size);
        foreach ($list['list'] as &$v){
            $phone =  $this->app->db->name('ai_user')->where(['id'=>$v['user_id']])->value("contact_phone");
            //隐藏手机号中间四位
            $v['phone'] = substr_replace($phone,'****',3,4);
        }
        $this->success('获取成功', $list);

    }



}
